home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / gcc.info-7 (.txt) < prev    next >
GNU Info File  |  1994-07-14  |  51KB  |  949 lines

  1. This is Info file gcc.info, produced by Makeinfo-1.54 from the input
  2. file gcc.texi.
  3.    This file documents the use and the internals of the GNU compiler.
  4.    Published by the Free Software Foundation 675 Massachusetts Avenue
  5. Cambridge, MA 02139 USA
  6.    Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
  7.    Permission is granted to make and distribute verbatim copies of this
  8. manual provided the copyright notice and this permission notice are
  9. preserved on all copies.
  10.    Permission is granted to copy and distribute modified versions of
  11. this manual under the conditions for verbatim copying, provided also
  12. that the sections entitled "GNU General Public License" and "Protect
  13. Your Freedom--Fight `Look And Feel'" are included exactly as in the
  14. original, and provided that the entire resulting derived work is
  15. distributed under the terms of a permission notice identical to this
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that the sections entitled "GNU General Public
  19. License" and "Protect Your Freedom--Fight `Look And Feel'", and this
  20. permission notice, may be included in translations approved by the Free
  21. Software Foundation instead of in the original English.
  22. File: gcc.info,  Node: VMS Install,  Next: Collect2,  Prev: Sun Install,  Up: Installation
  23. Installing GNU CC on VMS
  24. ========================
  25.    The VMS version of GNU CC is distributed in a backup saveset
  26. containing both source code and precompiled binaries.
  27.    To install the `gcc' command so you can use the compiler easily, in
  28. the same manner as you use the VMS C compiler, you must install the VMS
  29. CLD file for GNU CC as follows:
  30.   1. Define the VMS logical names `GNU_CC' and `GNU_CC_INCLUDE' to
  31.      point to the directories where the GNU CC executables
  32.      (`gcc-cpp.exe', `gcc-cc1.exe', etc.) and the C include files are
  33.      kept respectively.  This should be done with the commands:
  34.           $ assign /system /translation=concealed -
  35.             disk:[gcc.] gnu_cc
  36.           $ assign /system /translation=concealed -
  37.             disk:[gcc.include.] gnu_cc_include
  38.      with the appropriate disk and directory names.  These commands can
  39.      be placed in your system startup file so they will be executed
  40.      whenever the machine is rebooted.  You may, if you choose, do this
  41.      via the `GCC_INSTALL.COM' script in the `[GCC]' directory.
  42.   2. Install the `GCC' command with the command line:
  43.           $ set command /table=sys$common:[syslib]dcltables -
  44.             /output=sys$common:[syslib]dcltables gnu_cc:[000000]gcc
  45.           $ install replace sys$common:[syslib]dcltables
  46.   3. To install the help file, do the following:
  47.           $ library/help sys$library:helplib.hlb gcc.hlp
  48.      Now you can invoke the compiler with a command like `gcc /verbose
  49.      file.c', which is equivalent to the command `gcc -v -c file.c' in
  50.      Unix.
  51.    If you wish to use GNU C++ you must first install GNU CC, and then
  52. perform the following steps:
  53.   1. Define the VMS logical name `GNU_GXX_INCLUDE' to point to the
  54.      directory where the preprocessor will search for the C++ header
  55.      files.  This can be done with the command:
  56.           $ assign /system /translation=concealed -
  57.             disk:[gcc.gxx_include.] gnu_gxx_include
  58.      with the appropriate disk and directory name.  If you are going to
  59.      be using libg++, this is where the libg++ install procedure will
  60.      install the libg++ header files.
  61.   2. Obtain the file `gcc-cc1plus.exe', and place this in the same
  62.      directory that `gcc-cc1.exe' is kept.
  63.      The GNU C++ compiler can be invoked with a command like `gcc /plus
  64.      /verbose file.cc', which is equivalent to the command `g++ -v -c
  65.      file.cc' in Unix.
  66.    We try to put corresponding binaries and sources on the VMS
  67. distribution tape.  But sometimes the binaries will be from an older
  68. version than the sources, because we don't always have time to update
  69. them.  (Use the `/version' option to determine the version number of
  70. the binaries and compare it with the source file `version.c' to tell
  71. whether this is so.)  In this case, you should use the binaries you get
  72. to recompile the sources.  If you must recompile, here is how:
  73.   1. Execute the command procedure `vmsconfig.com' to set up the files
  74.      `tm.h', `config.h', `aux-output.c', and `md.', and to create files
  75.      `tconfig.h' and `hconfig.h'.  This procedure also creates several
  76.      linker option files used by `make-cc1.com' and a data file used by
  77.      `make-l2.com'.
  78.           $ @vmsconfig.com
  79.   2. Setup the logical names and command tables as defined above.  In
  80.      addition, define the VMS logical name `GNU_BISON' to point at the
  81.      to the directories where the Bison executable is kept.  This
  82.      should be done with the command:
  83.           $ assign /system /translation=concealed -
  84.             disk:[bison.] gnu_bison
  85.      You may, if you choose, use the `INSTALL_BISON.COM' script in the
  86.      `[BISON]' directory.
  87.   3. Install the `BISON' command with the command line:
  88.           $ set command /table=sys$common:[syslib]dcltables -
  89.             /output=sys$common:[syslib]dcltables -
  90.             gnu_bison:[000000]bison
  91.           $ install replace sys$common:[syslib]dcltables
  92.   4. Type `@make-gcc' to recompile everything (alternatively, submit
  93.      the file `make-gcc.com' to a batch queue).  If you wish to build
  94.      the GNU C++ compiler as well as the GNU CC compiler, you must
  95.      first edit `make-gcc.com' and follow the instructions that appear
  96.      in the comments.
  97.   5. In order to use GCC, you need a library of functions which GCC
  98.      compiled code will call to perform certain tasks, and these
  99.      functions are defined in the file `libgcc2.c'.  To compile this
  100.      you should use the command procedure `make-l2.com', which will
  101.      generate the library `libgcc2.olb'.  `libgcc2.olb' should be built
  102.      using the compiler built from the same distribution that
  103.      `libgcc2.c' came from, and `make-gcc.com' will automatically do
  104.      all of this for you.
  105.      To install the library, use the following commands:
  106.           $ library gnu_cc:[000000]gcclib/delete=(new,eprintf)
  107.           $ library gnu_cc:[000000]gcclib/delete=L_*
  108.           $ library libgcc2/extract=*/output=libgcc2.obj
  109.           $ library gnu_cc:[000000]gcclib libgcc2.obj
  110.      The first command simply removes old modules that will be replaced
  111.      with modules from `libgcc2' under different module names.  The
  112.      modules `new' and `eprintf' may not actually be present in your
  113.      `gcclib.olb'--if the VMS librarian complains about those modules
  114.      not being present, simply ignore the message and continue on with
  115.      the next command.  The second command removes the modules that
  116.      came from the previous version of the library `libgcc2.c'.
  117.      Whenever you update the compiler on your system, you should also
  118.      update the library with the above procedure.
  119.   6. You may wish to build GCC in such a way that no files are written
  120.      to the directory where the source files reside.  An example would
  121.      be the when the source files are on a read-only disk.  In these
  122.      cases, execute the following DCL commands (substituting your
  123.      actual path names):
  124.           $ assign dua0:[gcc.build_dir.]/translation=concealed, -
  125.                    dua1:[gcc.source_dir.]/translation=concealed  gcc_build
  126.           $ set default gcc_build:[000000]
  127.      where the directory `dua1:[gcc.source_dir]' contains the source
  128.      code, and the directory `dua0:[gcc.build_dir]' is meant to contain
  129.      all of the generated object files and executables.  Once you have
  130.      done this, you can proceed building GCC as described above.  (Keep
  131.      in mind that `gcc_build' is a rooted logical name, and thus the
  132.      device names in each element of the search list must be an actual
  133.      physical device name rather than another rooted logical name).
  134.   7. *If you are building GNU CC with a previous version of GNU CC, you
  135.      also should check to see that you have the newest version of the
  136.      assembler*.  In particular, GNU CC version 2 treats global constant
  137.      variables slightly differently from GNU CC version 1, and GAS
  138.      version 1.38.1 does not have the patches required to work with GCC
  139.      version 2.  If you use GAS 1.38.1, then `extern const' variables
  140.      will not have the read-only bit set, and the linker will generate
  141.      warning messages about mismatched psect attributes for these
  142.      variables.  These warning messages are merely a nuisance, and can
  143.      safely be ignored.
  144.      If you are compiling with a version of GNU CC older than 1.33,
  145.      specify `/DEFINE=("inline=")' as an option in all the
  146.      compilations.  This requires editing all the `gcc' commands in
  147.      `make-cc1.com'.  (The older versions had problems supporting
  148.      `inline'.)  Once you have a working 1.33 or newer GNU CC, you can
  149.      change this file back.
  150.   8. If you want to build GNU CC with the VAX C compiler, you will need
  151.      to make minor changes in `make-cccp.com' and `make-cc1.com' to
  152.      choose alternate definitions of `CC', `CFLAGS', and `LIBS'.  See
  153.      comments in those files.  However, you must also have a working
  154.      version of the GNU assembler (GNU as, aka GAS) as it is used as
  155.      the back-end for GNU CC to produce binary object modules and is
  156.      not included in the GNU CC sources.  GAS is also needed to compile
  157.      `libgcc2' in order to build `gcclib' (see above); `make-l2.com'
  158.      expects to be able to find it operational in
  159.      `gnu_cc:[000000]gnu-as.exe'.
  160.      To use GNU CC on VMS, you need the VMS driver programs `gcc.exe',
  161.      `gcc.com', and `gcc.cld'.  They are distributed with the VMS
  162.      binaries (`gcc-vms') rather than the GNU CC sources.  GAS is also
  163.      included in `gcc-vms', as is Bison.
  164.      Once you have successfully built GNU CC with VAX C, you should use
  165.      the resulting compiler to rebuild itself.  Before doing this, be
  166.      sure to restore the `CC', `CFLAGS', and `LIBS' definitions in
  167.      `make-cccp.com' and `make-cc1.com'.  The second generation
  168.      compiler will be able to take advantage of many optimizations that
  169.      must be suppressed when building with other compilers.
  170.    Under previous versions of GNU CC, the generated code would
  171. occasionally give strange results when linked with the sharable
  172. `VAXCRTL' library.  Now this should work.
  173.    Even with this version, however, GNU CC itself should not be linked
  174. with the sharable `VAXCRTL'.  The version of `qsort' in `VAXCRTL' has a
  175. bug (known to be present in VMS versions V4.6 through V5.5) which
  176. causes the compiler to fail.
  177.    The executables are generated by `make-cc1.com' and `make-cccp.com'
  178. use the object library version of `VAXCRTL' in order to make use of the
  179. `qsort' routine in `gcclib.olb'.  If you wish to link the compiler
  180. executables with the shareable image version of `VAXCRTL', you should
  181. edit the file `tm.h' (created by `vmsconfig.com') to define the macro
  182. `QSORT_WORKAROUND'.
  183.    `QSORT_WORKAROUND' is always defined when GNU CC is compiled with
  184. VAX C, to avoid a problem in case `gcclib.olb' is not yet available.
  185. File: gcc.info,  Node: Collect2,  Next: Header Dirs,  Prev: VMS Install,  Up: Installation
  186. `collect2'
  187. ==========
  188.    Many target systems do not have support in the assembler and linker
  189. for "constructors"--initialization functions to be called before the
  190. official "start" of `main'.  On such systems, GNU CC uses a utility
  191. called `collect2' to arrange to call these functions at start time.
  192.    The program `collect2' works by linking the program once and looking
  193. through the linker output file for symbols with particular names
  194. indicating they are constructor functions.  If it finds any, it creates
  195. a new temporary `.c' file containing a table of them, compiles it, and
  196. links the program a second time including that file.
  197.    The actual calls to the constructors are carried out by a subroutine
  198. called `__main', which is called (automatically) at the beginning of
  199. the body of `main' (provided `main' was compiled with GNU CC).  Calling
  200. `__main' is necessary, even when compiling C code, to allow linking C
  201. and C++ object code together.  (If you use `-nostdlib', you get an
  202. unresolved reference to `__main', since it's defined in the standard
  203. GCC library.  Include `-lgcc' at the end of your compiler command line
  204. to resolve this reference.)
  205.    The program `collect2' is installed as `ld' in the directory where
  206. the passes of the compiler are installed.  When `collect2' needs to
  207. find the *real* `ld', it tries the following file names:
  208.    * `real-ld' in the directories listed in the compiler's search
  209.      directories.
  210.    * `real-ld' in the directories listed in the environment variable
  211.      `PATH'.
  212.    * The file specified in the `REAL_LD_FILE_NAME' configuration macro,
  213.      if specified.
  214.    * `ld' in the compiler's search directories, except that `collect2'
  215.      will not execute itself recursively.
  216.    * `ld' in `PATH'.
  217.    "The compiler's search directories" means all the directories where
  218. `gcc' searches for passes of the compiler.  This includes directories
  219. that you specify with `-B'.
  220.    Cross-compilers search a little differently:
  221.    * `real-ld' in the compiler's search directories.
  222.    * `TARGET-real-ld' in `PATH'.
  223.    * The file specified in the `REAL_LD_FILE_NAME' configuration macro,
  224.      if specified.
  225.    * `ld' in the compiler's search directories.
  226.    * `TARGET-ld' in `PATH'.
  227.    `collect2' explicitly avoids running `ld' using the file name under
  228. which `collect2' itself was invoked.  In fact, it remembers up a list
  229. of such names--in case one copy of `collect2' finds another copy (or
  230. version) of `collect2' installed as `ld' in a second place in the
  231. search path.
  232.    `collect2' searches for the utilities `nm' and `strip' using the
  233. same algorithm as above for `ld'.
  234. File: gcc.info,  Node: Header Dirs,  Prev: Collect2,  Up: Installation
  235. Standard Header File Directories
  236. ================================
  237.    `GCC_INCLUDE_DIR' means the same thing for native and cross.  It is
  238. where GNU CC stores its private include files, and also where GNU CC
  239. stores the fixed include files.  A cross compiled GNU CC runs
  240. `fixincludes' on the header files in `$(tooldir)/include'.  (If the
  241. cross compilation header files need to be fixed, they must be installed
  242. before GNU CC is built.  If the cross compilation header files are
  243. already suitable for ANSI C and GNU CC, nothing special need be done).
  244.    `GPLUS_INCLUDE_DIR' means the same thing for native and cross.  It
  245. is where `g++' looks first for header files.  `libg++' installs only
  246. target independent header files in that directory.
  247.    `LOCAL_INCLUDE_DIR' is used only for a native compiler.  It is
  248. normally `/usr/local/include'.  GNU CC searches this directory so that
  249. users can install header files in `/usr/local/include'.
  250.    `CROSS_INCLUDE_DIR' is used only for a cross compiler.  GNU CC
  251. doesn't install anything there.
  252.    `TOOL_INCLUDE_DIR' is used for both native and cross compilers.  It
  253. is the place for other packages to install header files that GNU CC will
  254. use.  For a cross-compiler, this is the equivalent of `/usr/include'.
  255. When you build a cross-compiler, `fixincludes' processes any header
  256. files in this directory.
  257. File: gcc.info,  Node: C Extensions,  Next: C++ Extensions,  Prev: Installation,  Up: Top
  258. Extensions to the C Language Family
  259. ***********************************
  260.    GNU C provides several language features not found in ANSI standard
  261. C.  (The `-pedantic' option directs GNU CC to print a warning message if
  262. any of these features is used.)  To test for the availability of these
  263. features in conditional compilation, check for a predefined macro
  264. `__GNUC__', which is always defined under GNU CC.
  265.    These extensions are available in C and in the languages derived from
  266. it, C++ and Objective C.  *Note Extensions to the C++ Language: C++
  267. Extensions, for extensions that apply *only* to C++.
  268. * Menu:
  269. * Statement Exprs::     Putting statements and declarations inside expressions.
  270. * Local Labels::        Labels local to a statement-expression.
  271. * Labels as Values::    Getting pointers to labels, and computed gotos.
  272. * Nested Functions::    As in Algol and Pascal, lexical scoping of functions.
  273. * Constructing Calls::    Dispatching a call to another function.
  274. * Naming Types::        Giving a name to the type of some expression.
  275. * Typeof::              `typeof': referring to the type of an expression.
  276. * Lvalues::             Using `?:', `,' and casts in lvalues.
  277. * Conditionals::        Omitting the middle operand of a `?:' expression.
  278. * Long Long::        Double-word integers--`long long int'.
  279. * Complex::             Data types for complex numbers.
  280. * Zero Length::         Zero-length arrays.
  281. * Variable Length::     Arrays whose length is computed at run time.
  282. * Macro Varargs::    Macros with variable number of arguments.
  283. * Subscripting::        Any array can be subscripted, even if not an lvalue.
  284. * Pointer Arith::       Arithmetic on `void'-pointers and function pointers.
  285. * Initializers::        Non-constant initializers.
  286. * Constructors::        Constructor expressions give structures, unions
  287.                          or arrays as values.
  288. * Labeled Elements::    Labeling elements of initializers.
  289. * Cast to Union::       Casting to union type from any member of the union.
  290. * Case Ranges::        `case 1 ... 9' and such.
  291. * Function Attributes:: Declaring that functions have no side effects,
  292.                          or that they can never return.
  293. * Function Prototypes:: Prototype declarations and old-style definitions.
  294. * Dollar Signs::        Dollar sign is allowed in identifiers.
  295. * Character Escapes::   `\e' stands for the character ESC.
  296. * Variable Attributes::    Specifying attributes of variables.
  297. * Alignment::           Inquiring about the alignment of a type or variable.
  298. * Inline::              Defining inline functions (as fast as macros).
  299. * Extended Asm::        Assembler instructions with C expressions as operands.
  300.                          (With them you can define "built-in" functions.)
  301. * Asm Labels::          Specifying the assembler name to use for a C symbol.
  302. * Explicit Reg Vars::   Defining variables residing in specified registers.
  303. * Alternate Keywords::  `__const__', `__asm__', etc., for header files.
  304. * Incomplete Enums::    `enum foo;', with details to follow.
  305. * Function Names::    Printable strings which are the name of the current
  306.              function.
  307. File: gcc.info,  Node: Statement Exprs,  Next: Local Labels,  Up: C Extensions
  308. Statements and Declarations in Expressions
  309. ==========================================
  310.    A compound statement enclosed in parentheses may appear as an
  311. expression in GNU C.  This allows you to use loops, switches, and local
  312. variables within an expression.
  313.    Recall that a compound statement is a sequence of statements
  314. surrounded by braces; in this construct, parentheses go around the
  315. braces.  For example:
  316.      ({ int y = foo (); int z;
  317.         if (y > 0) z = y;
  318.         else z = - y;
  319.         z; })
  320. is a valid (though slightly more complex than necessary) expression for
  321. the absolute value of `foo ()'.
  322.    The last thing in the compound statement should be an expression
  323. followed by a semicolon; the value of this subexpression serves as the
  324. value of the entire construct.  (If you use some other kind of statement
  325. last within the braces, the construct has type `void', and thus
  326. effectively no value.)
  327.    This feature is especially useful in making macro definitions "safe"
  328. (so that they evaluate each operand exactly once).  For example, the
  329. "maximum" function is commonly defined as a macro in standard C as
  330. follows:
  331.      #define max(a,b) ((a) > (b) ? (a) : (b))
  332. But this definition computes either A or B twice, with bad results if
  333. the operand has side effects.  In GNU C, if you know the type of the
  334. operands (here let's assume `int'), you can define the macro safely as
  335. follows:
  336.      #define maxint(a,b) \
  337.        ({int _a = (a), _b = (b); _a > _b ? _a : _b; })
  338.    Embedded statements are not allowed in constant expressions, such as
  339. the value of an enumeration constant, the width of a bit field, or the
  340. initial value of a static variable.
  341.    If you don't know the type of the operand, you can still do this,
  342. but you must use `typeof' (*note Typeof::.) or type naming (*note
  343. Naming Types::.).
  344. File: gcc.info,  Node: Local Labels,  Next: Labels as Values,  Prev: Statement Exprs,  Up: C Extensions
  345. Locally Declared Labels
  346. =======================
  347.    Each statement expression is a scope in which "local labels" can be
  348. declared.  A local label is simply an identifier; you can jump to it
  349. with an ordinary `goto' statement, but only from within the statement
  350. expression it belongs to.
  351.    A local label declaration looks like this:
  352.      __label__ LABEL;
  353.      __label__ LABEL1, LABEL2, ...;
  354.    Local label declarations must come at the beginning of the statement
  355. expression, right after the `({', before any ordinary declarations.
  356.    The label declaration defines the label *name*, but does not define
  357. the label itself.  You must do this in the usual way, with `LABEL:',
  358. within the statements of the statement expression.
  359.    The local label feature is useful because statement expressions are
  360. often used in macros.  If the macro contains nested loops, a `goto' can
  361. be useful for breaking out of them.  However, an ordinary label whose
  362. scope is the whole function cannot be used: if the macro can be
  363. expanded several times in one function, the label will be multiply
  364. defined in that function.  A local label avoids this problem.  For
  365. example:
  366.      #define SEARCH(array, target)                     \
  367.      ({                                               \
  368.        __label__ found;                                \
  369.        typeof (target) _SEARCH_target = (target);      \
  370.        typeof (*(array)) *_SEARCH_array = (array);     \
  371.        int i, j;                                       \
  372.        int value;                                      \
  373.        for (i = 0; i < max; i++)                       \
  374.          for (j = 0; j < max; j++)                     \
  375.            if (_SEARCH_array[i][j] == _SEARCH_target)  \
  376.              { value = i; goto found; }              \
  377.        value = -1;                                     \
  378.       found:                                           \
  379.        value;                                          \
  380.      })
  381. File: gcc.info,  Node: Labels as Values,  Next: Nested Functions,  Prev: Local Labels,  Up: C Extensions
  382. Labels as Values
  383. ================
  384.    You can get the address of a label defined in the current function
  385. (or a containing function) with the unary operator `&&'.  The value has
  386. type `void *'.  This value is a constant and can be used wherever a
  387. constant of that type is valid.  For example:
  388.      void *ptr;
  389.      ...
  390.      ptr = &&foo;
  391.    To use these values, you need to be able to jump to one.  This is
  392. done with the computed goto statement(1), `goto *EXP;'.  For example,
  393.      goto *ptr;
  394. Any expression of type `void *' is allowed.
  395.    One way of using these constants is in initializing a static array
  396. that will serve as a jump table:
  397.      static void *array[] = { &&foo, &&bar, &&hack };
  398.    Then you can select a label with indexing, like this:
  399.      goto *array[i];
  400. Note that this does not check whether the subscript is in bounds--array
  401. indexing in C never does that.
  402.    Such an array of label values serves a purpose much like that of the
  403. `switch' statement.  The `switch' statement is cleaner, so use that
  404. rather than an array unless the problem does not fit a `switch'
  405. statement very well.
  406.    Another use of label values is in an interpreter for threaded code.
  407. The labels within the interpreter function can be stored in the
  408. threaded code for super-fast dispatching.
  409.    You can use this mechanism to jump to code in a different function.
  410. If you do that, totally unpredictable things will happen.  The best way
  411. to avoid this is to store the label address only in automatic variables
  412. and never pass it as an argument.
  413.    ---------- Footnotes ----------
  414.    (1)  The analogous feature in Fortran is called an assigned goto,
  415. but that name seems inappropriate in C, where one can do more than
  416. simply store label addresses in label variables.
  417. File: gcc.info,  Node: Nested Functions,  Next: Constructing Calls,  Prev: Labels as Values,  Up: C Extensions
  418. Nested Functions
  419. ================
  420.    A "nested function" is a function defined inside another function.
  421. (Nested functions are not supported for GNU C++.)  The nested function's
  422. name is local to the block where it is defined.  For example, here we
  423. define a nested function named `square', and call it twice:
  424.      foo (double a, double b)
  425.      {
  426.        double square (double z) { return z * z; }
  427.      
  428.        return square (a) + square (b);
  429.      }
  430.    The nested function can access all the variables of the containing
  431. function that are visible at the point of its definition.  This is
  432. called "lexical scoping".  For example, here we show a nested function
  433. which uses an inherited variable named `offset':
  434.      bar (int *array, int offset, int size)
  435.      {
  436.        int access (int *array, int index)
  437.          { return array[index + offset]; }
  438.        int i;
  439.        ...
  440.        for (i = 0; i < size; i++)
  441.          ... access (array, i) ...
  442.      }
  443.    Nested function definitions are permitted within functions in the
  444. places where variable definitions are allowed; that is, in any block,
  445. before the first statement in the block.
  446.    It is possible to call the nested function from outside the scope of
  447. its name by storing its address or passing the address to another
  448. function:
  449.      hack (int *array, int size)
  450.      {
  451.        void store (int index, int value)
  452.          { array[index] = value; }
  453.      
  454.        intermediate (store, size);
  455.      }
  456.    Here, the function `intermediate' receives the address of `store' as
  457. an argument.  If `intermediate' calls `store', the arguments given to
  458. `store' are used to store into `array'.  But this technique works only
  459. so long as the containing function (`hack', in this example) does not
  460. exit.
  461.    If you try to call the nested function through its address after the
  462. containing function has exited, all hell will break loose.  If you try
  463. to call it after a containing scope level has exited, and if it refers
  464. to some of the variables that are no longer in scope, you may be lucky,
  465. but it's not wise to take the risk.  If, however, the nested function
  466. does not refer to anything that has gone out of scope, you should be
  467. safe.
  468.    GNU CC implements taking the address of a nested function using a
  469. technique called "trampolines".  A paper describing them is available
  470. from `maya.idiap.ch' in directory `pub/tmb', file `usenix88-lexic.ps.Z'.
  471.    A nested function can jump to a label inherited from a containing
  472. function, provided the label was explicitly declared in the containing
  473. function (*note Local Labels::.).  Such a jump returns instantly to the
  474. containing function, exiting the nested function which did the `goto'
  475. and any intermediate functions as well.  Here is an example:
  476.      bar (int *array, int offset, int size)
  477.      {
  478.        __label__ failure;
  479.        int access (int *array, int index)
  480.          {
  481.            if (index > size)
  482.              goto failure;
  483.            return array[index + offset];
  484.          }
  485.        int i;
  486.        ...
  487.        for (i = 0; i < size; i++)
  488.          ... access (array, i) ...
  489.        ...
  490.        return 0;
  491.      
  492.       /* Control comes here from `access'
  493.          if it detects an error.  */
  494.       failure:
  495.        return -1;
  496.      }
  497.    A nested function always has internal linkage.  Declaring one with
  498. `extern' is erroneous.  If you need to declare the nested function
  499. before its definition, use `auto' (which is otherwise meaningless for
  500. function declarations).
  501.      bar (int *array, int offset, int size)
  502.      {
  503.        __label__ failure;
  504.        auto int access (int *, int);
  505.        ...
  506.        int access (int *array, int index)
  507.          {
  508.            if (index > size)
  509.              goto failure;
  510.            return array[index + offset];
  511.          }
  512.        ...
  513.      }
  514. File: gcc.info,  Node: Constructing Calls,  Next: Naming Types,  Prev: Nested Functions,  Up: C Extensions
  515. Constructing Function Calls
  516. ===========================
  517.    Using the built-in functions described below, you can record the
  518. arguments a function received, and call another function with the same
  519. arguments, without knowing the number or types of the arguments.
  520.    You can also record the return value of that function call, and
  521. later return that value, without knowing what data type the function
  522. tried to return (as long as your caller expects that data type).
  523. `__builtin_apply_args ()'
  524.      This built-in function returns a pointer of type `void *' to data
  525.      describing how to perform a call with the same arguments as were
  526.      passed to the current function.
  527.      The function saves the arg pointer register, structure value
  528.      address, and all registers that might be used to pass arguments to
  529.      a function into a block of memory allocated on the stack.  Then it
  530.      returns the address of that block.
  531. `__builtin_apply (FUNCTION, ARGUMENTS, SIZE)'
  532.      This built-in function invokes FUNCTION (type `void (*)()') with a
  533.      copy of the parameters described by ARGUMENTS (type `void *') and
  534.      SIZE (type `int').
  535.      The value of ARGUMENTS should be the value returned by
  536.      `__builtin_apply_args'.  The argument SIZE specifies the size of
  537.      the stack argument data, in bytes.
  538.      This function returns a pointer of type `void *' to data describing
  539.      how to return whatever value was returned by FUNCTION.  The data
  540.      is saved in a block of memory allocated on the stack.
  541.      It is not always simple to compute the proper value for SIZE.  The
  542.      value is used by `__builtin_apply' to compute the amount of data
  543.      that should be pushed on the stack and copied from the incoming
  544.      argument area.
  545. `__builtin_return (RESULT)'
  546.      This built-in function returns the value described by RESULT from
  547.      the containing function.  You should specify, for RESULT, a value
  548.      returned by `__builtin_apply'.
  549. File: gcc.info,  Node: Naming Types,  Next: Typeof,  Prev: Constructing Calls,  Up: C Extensions
  550. Naming an Expression's Type
  551. ===========================
  552.    You can give a name to the type of an expression using a `typedef'
  553. declaration with an initializer.  Here is how to define NAME as a type
  554. name for the type of EXP:
  555.      typedef NAME = EXP;
  556.    This is useful in conjunction with the statements-within-expressions
  557. feature.  Here is how the two together can be used to define a safe
  558. "maximum" macro that operates on any arithmetic type:
  559.      #define max(a,b) \
  560.        ({typedef _ta = (a), _tb = (b);  \
  561.          _ta _a = (a); _tb _b = (b);     \
  562.          _a > _b ? _a : _b; })
  563.    The reason for using names that start with underscores for the local
  564. variables is to avoid conflicts with variable names that occur within
  565. the expressions that are substituted for `a' and `b'.  Eventually we
  566. hope to design a new form of declaration syntax that allows you to
  567. declare variables whose scopes start only after their initializers;
  568. this will be a more reliable way to prevent such conflicts.
  569. File: gcc.info,  Node: Typeof,  Next: Lvalues,  Prev: Naming Types,  Up: C Extensions
  570. Referring to a Type with `typeof'
  571. =================================
  572.    Another way to refer to the type of an expression is with `typeof'.
  573. The syntax of using of this keyword looks like `sizeof', but the
  574. construct acts semantically like a type name defined with `typedef'.
  575.    There are two ways of writing the argument to `typeof': with an
  576. expression or with a type.  Here is an example with an expression:
  577.      typeof (x[0](1))
  578. This assumes that `x' is an array of functions; the type described is
  579. that of the values of the functions.
  580.    Here is an example with a typename as the argument:
  581.      typeof (int *)
  582. Here the type described is that of pointers to `int'.
  583.    If you are writing a header file that must work when included in
  584. ANSI C programs, write `__typeof__' instead of `typeof'.  *Note
  585. Alternate Keywords::.
  586.    A `typeof'-construct can be used anywhere a typedef name could be
  587. used.  For example, you can use it in a declaration, in a cast, or
  588. inside of `sizeof' or `typeof'.
  589.    * This declares `y' with the type of what `x' points to.
  590.           typeof (*x) y;
  591.    * This declares `y' as an array of such values.
  592.           typeof (*x) y[4];
  593.    * This declares `y' as an array of pointers to characters:
  594.           typeof (typeof (char *)[4]) y;
  595.      It is equivalent to the following traditional C declaration:
  596.           char *y[4];
  597.      To see the meaning of the declaration using `typeof', and why it
  598.      might be a useful way to write, let's rewrite it with these macros:
  599.           #define pointer(T)  typeof(T *)
  600.           #define array(T, N) typeof(T [N])
  601.      Now the declaration can be rewritten this way:
  602.           array (pointer (char), 4) y;
  603.      Thus, `array (pointer (char), 4)' is the type of arrays of 4
  604.      pointers to `char'.
  605. File: gcc.info,  Node: Lvalues,  Next: Conditionals,  Prev: Typeof,  Up: C Extensions
  606. Generalized Lvalues
  607. ===================
  608.    Compound expressions, conditional expressions and casts are allowed
  609. as lvalues provided their operands are lvalues.  This means that you
  610. can take their addresses or store values into them.
  611.    For example, a compound expression can be assigned, provided the last
  612. expression in the sequence is an lvalue.  These two expressions are
  613. equivalent:
  614.      (a, b) += 5
  615.      a, (b += 5)
  616.    Similarly, the address of the compound expression can be taken.
  617. These two expressions are equivalent:
  618.      &(a, b)
  619.      a, &b
  620.    A conditional expression is a valid lvalue if its type is not void
  621. and the true and false branches are both valid lvalues.  For example,
  622. these two expressions are equivalent:
  623.      (a ? b : c) = 5
  624.      (a ? b = 5 : (c = 5))
  625.    A cast is a valid lvalue if its operand is an lvalue.  A simple
  626. assignment whose left-hand side is a cast works by converting the
  627. right-hand side first to the specified type, then to the type of the
  628. inner left-hand side expression.  After this is stored, the value is
  629. converted back to the specified type to become the value of the
  630. assignment.  Thus, if `a' has type `char *', the following two
  631. expressions are equivalent:
  632.      (int)a = 5
  633.      (int)(a = (char *)(int)5)
  634.    An assignment-with-arithmetic operation such as `+=' applied to a
  635. cast performs the arithmetic using the type resulting from the cast,
  636. and then continues as in the previous case.  Therefore, these two
  637. expressions are equivalent:
  638.      (int)a += 5
  639.      (int)(a = (char *)(int) ((int)a + 5))
  640.    You cannot take the address of an lvalue cast, because the use of its
  641. address would not work out coherently.  Suppose that `&(int)f' were
  642. permitted, where `f' has type `float'.  Then the following statement
  643. would try to store an integer bit-pattern where a floating point number
  644. belongs:
  645.      *&(int)f = 1;
  646.    This is quite different from what `(int)f = 1' would do--that would
  647. convert 1 to floating point and store it.  Rather than cause this
  648. inconsistency, we think it is better to prohibit use of `&' on a cast.
  649.    If you really do want an `int *' pointer with the address of `f',
  650. you can simply write `(int *)&f'.
  651. File: gcc.info,  Node: Conditionals,  Next: Long Long,  Prev: Lvalues,  Up: C Extensions
  652. Conditionals with Omitted Operands
  653. ==================================
  654.    The middle operand in a conditional expression may be omitted.  Then
  655. if the first operand is nonzero, its value is the value of the
  656. conditional expression.
  657.    Therefore, the expression
  658.      x ? : y
  659. has the value of `x' if that is nonzero; otherwise, the value of `y'.
  660.    This example is perfectly equivalent to
  661.      x ? x : y
  662. In this simple case, the ability to omit the middle operand is not
  663. especially useful.  When it becomes useful is when the first operand
  664. does, or may (if it is a macro argument), contain a side effect.  Then
  665. repeating the operand in the middle would perform the side effect
  666. twice.  Omitting the middle operand uses the value already computed
  667. without the undesirable effects of recomputing it.
  668. File: gcc.info,  Node: Long Long,  Next: Complex,  Prev: Conditionals,  Up: C Extensions
  669. Double-Word Integers
  670. ====================
  671.    GNU C supports data types for integers that are twice as long as
  672. `long int'.  Simply write `long long int' for a signed integer, or
  673. `unsigned long long int' for an unsigned integer.  To make an integer
  674. constant of type `long long int', add the suffix `LL' to the integer.
  675. To make an integer constant of type `unsigned long long int', add the
  676. suffix `ULL' to the integer.
  677.    You can use these types in arithmetic like any other integer types.
  678. Addition, subtraction, and bitwise boolean operations on these types
  679. are open-coded on all types of machines.  Multiplication is open-coded
  680. if the machine supports fullword-to-doubleword a widening multiply
  681. instruction.  Division and shifts are open-coded only on machines that
  682. provide special support.  The operations that are not open-coded use
  683. special library routines that come with GNU CC.
  684.    There may be pitfalls when you use `long long' types for function
  685. arguments, unless you declare function prototypes.  If a function
  686. expects type `int' for its argument, and you pass a value of type `long
  687. long int', confusion will result because the caller and the subroutine
  688. will disagree about the number of bytes for the argument.  Likewise, if
  689. the function expects `long long int' and you pass `int'.  The best way
  690. to avoid such problems is to use prototypes.
  691. File: gcc.info,  Node: Complex,  Next: Zero Length,  Prev: Long Long,  Up: C Extensions
  692. Complex Numbers
  693. ===============
  694.    GNU C supports complex data types.  You can declare both complex
  695. integer types and complex floating types, using the keyword
  696. `__complex__'.
  697.    For example, `__complex__ double x;' declares `x' as a variable
  698. whose real part and imaginary part are both of type `double'.
  699. `__complex__ short int y;' declares `y' to have real and imaginary
  700. parts of type `short int'; this is not likely to be useful, but it
  701. shows that the set of complex types is complete.
  702.    To write a constant with a complex data type, use the suffix `i' or
  703. `j' (either one; they are equivalent).  For example, `2.5fi' has type
  704. `__complex__ float' and `3i' has type `__complex__ int'.  Such a
  705. constant always has a pure imaginary value, but you can form any
  706. complex value you like by adding one to a real constant.
  707.    To extract the real part of a complex-valued expression EXP, write
  708. `__real__ EXP'.  Likewise, use `__imag__' to extract the imaginary part.
  709.    The operator `~' performs complex conjugation when used on a value
  710. with a complex type.
  711.    GNU CC can allocate complex automatic variables in a noncontiguous
  712. fashion; it's even possible for the real part to be in a register while
  713. the imaginary part is on the stack (or vice-versa).  None of the
  714. supported debugging info formats has a way to represent noncontiguous
  715. allocation like this, so GNU CC describes a noncontiguous complex
  716. variable as if it were two separate variables of noncomplex type.  If
  717. the variable's actual name is `foo', the two fictitious variables are
  718. named `foo$real' and `foo$imag'.  You can examine and set these two
  719. fictitious variables with your debugger.
  720.    A future version of GDB will know how to recognize such pairs and
  721. treat them as a single variable with a complex type.
  722. File: gcc.info,  Node: Zero Length,  Next: Variable Length,  Prev: Complex,  Up: C Extensions
  723. Arrays of Length Zero
  724. =====================
  725.    Zero-length arrays are allowed in GNU C.  They are very useful as
  726. the last element of a structure which is really a header for a
  727. variable-length object:
  728.      struct line {
  729.        int length;
  730.        char contents[0];
  731.      };
  732.      
  733.      {
  734.        struct line *thisline = (struct line *)
  735.          malloc (sizeof (struct line) + this_length);
  736.        thisline->length = this_length;
  737.      }
  738.    In standard C, you would have to give `contents' a length of 1, which
  739. means either you waste space or complicate the argument to `malloc'.
  740. File: gcc.info,  Node: Variable Length,  Next: Macro Varargs,  Prev: Zero Length,  Up: C Extensions
  741. Arrays of Variable Length
  742. =========================
  743.    Variable-length automatic arrays are allowed in GNU C.  These arrays
  744. are declared like any other automatic arrays, but with a length that is
  745. not a constant expression.  The storage is allocated at the point of
  746. declaration and deallocated when the brace-level is exited.  For
  747. example:
  748.      FILE *
  749.      concat_fopen (char *s1, char *s2, char *mode)
  750.      {
  751.        char str[strlen (s1) + strlen (s2) + 1];
  752.        strcpy (str, s1);
  753.        strcat (str, s2);
  754.        return fopen (str, mode);
  755.      }
  756.    Jumping or breaking out of the scope of the array name deallocates
  757. the storage.  Jumping into the scope is not allowed; you get an error
  758. message for it.
  759.    You can use the function `alloca' to get an effect much like
  760. variable-length arrays.  The function `alloca' is available in many
  761. other C implementations (but not in all).  On the other hand,
  762. variable-length arrays are more elegant.
  763.    There are other differences between these two methods.  Space
  764. allocated with `alloca' exists until the containing *function* returns.
  765. The space for a variable-length array is deallocated as soon as the
  766. array name's scope ends.  (If you use both variable-length arrays and
  767. `alloca' in the same function, deallocation of a variable-length array
  768. will also deallocate anything more recently allocated with `alloca'.)
  769.    You can also use variable-length arrays as arguments to functions:
  770.      struct entry
  771.      tester (int len, char data[len][len])
  772.      {
  773.        ...
  774.      }
  775.    The length of an array is computed once when the storage is allocated
  776. and is remembered for the scope of the array in case you access it with
  777. `sizeof'.
  778.    If you want to pass the array first and the length afterward, you can
  779. use a forward declaration in the parameter list--another GNU extension.
  780.      struct entry
  781.      tester (int len; char data[len][len], int len)
  782.      {
  783.        ...
  784.      }
  785.    The `int len' before the semicolon is a "parameter forward
  786. declaration", and it serves the purpose of making the name `len' known
  787. when the declaration of `data' is parsed.
  788.    You can write any number of such parameter forward declarations in
  789. the parameter list.  They can be separated by commas or semicolons, but
  790. the last one must end with a semicolon, which is followed by the "real"
  791. parameter declarations.  Each forward declaration must match a "real"
  792. declaration in parameter name and data type.
  793. File: gcc.info,  Node: Macro Varargs,  Next: Subscripting,  Prev: Variable Length,  Up: C Extensions
  794. Macros with Variable Numbers of Arguments
  795. =========================================
  796.    In GNU C, a macro can accept a variable number of arguments, much as
  797. a function can.  The syntax for defining the macro looks much like that
  798. used for a function.  Here is an example:
  799.      #define eprintf(format, args...)  \
  800.       fprintf (stderr, format , ## args)
  801.    Here `args' is a "rest argument": it takes in zero or more
  802. arguments, as many as the call contains.  All of them plus the commas
  803. between them form the value of `args', which is substituted into the
  804. macro body where `args' is used.  Thus, we have this expansion:
  805.      eprintf ("%s:%d: ", input_file_name, line_number)
  806.      ==>
  807.      fprintf (stderr, "%s:%d: " , input_file_name, line_number)
  808. Note that the comma after the string constant comes from the definition
  809. of `eprintf', whereas the last comma comes from the value of `args'.
  810.    The reason for using `##' is to handle the case when `args' matches
  811. no arguments at all.  In this case, `args' has an empty value.  In this
  812. case, the second comma in the definition becomes an embarrassment: if
  813. it got through to the expansion of the macro, we would get something
  814. like this:
  815.      fprintf (stderr, "success!\n" , )
  816. which is invalid C syntax.  `##' gets rid of the comma, so we get the
  817. following instead:
  818.      fprintf (stderr, "success!\n")
  819.    This is a special feature of the GNU C preprocessor: `##' before a
  820. rest argument that is empty discards the preceding sequence of
  821. non-whitespace characters from the macro definition.  (If another macro
  822. argument precedes, none of it is discarded.)
  823.    It might be better to discard the last preprocessor token instead of
  824. the last preceding sequence of non-whitespace characters; in fact, we
  825. may someday change this feature to do so.  We advise you to write the
  826. macro definition so that the preceding sequence of non-whitespace
  827. characters is just a single token, so that the meaning will not change
  828. if we change the definition of this feature.
  829. File: gcc.info,  Node: Subscripting,  Next: Pointer Arith,  Prev: Macro Varargs,  Up: C Extensions
  830. Non-Lvalue Arrays May Have Subscripts
  831. =====================================
  832.    Subscripting is allowed on arrays that are not lvalues, even though
  833. the unary `&' operator is not.  For example, this is valid in GNU C
  834. though not valid in other C dialects:
  835.      struct foo {int a[4];};
  836.      
  837.      struct foo f();
  838.      
  839.      bar (int index)
  840.      {
  841.        return f().a[index];
  842.      }
  843. File: gcc.info,  Node: Pointer Arith,  Next: Initializers,  Prev: Subscripting,  Up: C Extensions
  844. Arithmetic on `void'- and Function-Pointers
  845. ===========================================
  846.    In GNU C, addition and subtraction operations are supported on
  847. pointers to `void' and on pointers to functions.  This is done by
  848. treating the size of a `void' or of a function as 1.
  849.    A consequence of this is that `sizeof' is also allowed on `void' and
  850. on function types, and returns 1.
  851.    The option `-Wpointer-arith' requests a warning if these extensions
  852. are used.
  853. File: gcc.info,  Node: Initializers,  Next: Constructors,  Prev: Pointer Arith,  Up: C Extensions
  854. Non-Constant Initializers
  855. =========================
  856.    The elements of an aggregate initializer for an automatic variable
  857. are not required to be constant expressions in GNU C.  Here is an
  858. example of an initializer with run-time varying elements:
  859.      foo (float f, float g)
  860.      {
  861.        float beat_freqs[2] = { f-g, f+g };
  862.        ...
  863.      }
  864. File: gcc.info,  Node: Constructors,  Next: Labeled Elements,  Prev: Initializers,  Up: C Extensions
  865. Constructor Expressions
  866. =======================
  867.    GNU C supports constructor expressions.  A constructor looks like a
  868. cast containing an initializer.  Its value is an object of the type
  869. specified in the cast, containing the elements specified in the
  870. initializer.
  871.    Usually, the specified type is a structure.  Assume that `struct
  872. foo' and `structure' are declared as shown:
  873.      struct foo {int a; char b[2];} structure;
  874. Here is an example of constructing a `struct foo' with a constructor:
  875.      structure = ((struct foo) {x + y, 'a', 0});
  876. This is equivalent to writing the following:
  877.      {
  878.        struct foo temp = {x + y, 'a', 0};
  879.        structure = temp;
  880.      }
  881.    You can also construct an array.  If all the elements of the
  882. constructor are (made up of) simple constant expressions, suitable for
  883. use in initializers, then the constructor is an lvalue and can be
  884. coerced to a pointer to its first element, as shown here:
  885.      char **foo = (char *[]) { "x", "y", "z" };
  886.    Array constructors whose elements are not simple constants are not
  887. very useful, because the constructor is not an lvalue.  There are only
  888. two valid ways to use it: to subscript it, or initialize an array
  889. variable with it.  The former is probably slower than a `switch'
  890. statement, while the latter does the same thing an ordinary C
  891. initializer would do.  Here is an example of subscripting an array
  892. constructor:
  893.      output = ((int[]) { 2, x, 28 }) [input];
  894.    Constructor expressions for scalar types and union types are is also
  895. allowed, but then the constructor expression is equivalent to a cast.
  896. File: gcc.info,  Node: Labeled Elements,  Next: Cast to Union,  Prev: Constructors,  Up: C Extensions
  897. Labeled Elements in Initializers
  898. ================================
  899.    Standard C requires the elements of an initializer to appear in a
  900. fixed order, the same as the order of the elements in the array or
  901. structure being initialized.
  902.    In GNU C you can give the elements in any order, specifying the array
  903. indices or structure field names they apply to.
  904.    To specify an array index, write `[INDEX]' or `[INDEX] =' before the
  905. element value.  For example,
  906.      int a[6] = { [4] 29, [2] = 15 };
  907. is equivalent to
  908.      int a[6] = { 0, 0, 15, 0, 29, 0 };
  909. The index values must be constant expressions, even if the array being
  910. initialized is automatic.
  911.    To initialize a range of elements to the same value, write `[FIRST
  912. ... LAST] = VALUE'.  For example,
  913.      int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
  914. Note that the length of the array is the highest value specified plus
  915.    In a structure initializer, specify the name of a field to initialize
  916. with `FIELDNAME:' before the element value.  For example, given the
  917. following structure,
  918.      struct point { int x, y; };
  919. the following initialization
  920.      struct point p = { y: yvalue, x: xvalue };
  921. is equivalent to
  922.      struct point p = { xvalue, yvalue };
  923.    Another syntax which has the same meaning is `.FIELDNAME ='., as
  924. shown here:
  925.      struct point p = { .y = yvalue, .x = xvalue };
  926.    You can also use an element label (with either the colon syntax or
  927. the period-equal syntax) when initializing a union, to specify which
  928. element of the union should be used.  For example,
  929.      union foo { int i; double d; };
  930.      
  931.      union foo f = { d: 4 };
  932. will convert 4 to a `double' to store it in the union using the second
  933. element.  By contrast, casting 4 to type `union foo' would store it
  934. into the union as the integer `i', since it is an integer.  (*Note Cast
  935. to Union::.)
  936.    You can combine this technique of naming elements with ordinary C
  937. initialization of successive elements.  Each initializer element that
  938. does not have a label applies to the next consecutive element of the
  939. array or structure.  For example,
  940.      int a[6] = { [1] = v1, v2, [4] = v4 };
  941. is equivalent to
  942.      int a[6] = { 0, v1, v2, 0, v4, 0 };
  943.    Labeling the elements of an array initializer is especially useful
  944. when the indices are characters or belong to an `enum' type.  For
  945. example:
  946.      int whitespace[256]
  947.        = { [' '] = 1, ['\t'] = 1, ['\h'] = 1,
  948.            ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
  949.